<?PHP
/**
 * LianPHP工具类库
 * 
 * @author 大春 lianwanchun@163.com
 * @version 0.1 
 * @copyright Copyright &copy; 2010 lian wan chun
 */

class Tool extends LianPhp {

	/**
	 * 获取客户段IP
	 *
	 * @return string
	 */
	public function getClientIP(){
		if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		$ip = getenv("HTTP_CLIENT_IP");
		else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
		else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		$ip = getenv("REMOTE_ADDR");
		else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		$ip = $_SERVER['REMOTE_ADDR'];
		else
		return false;
		return($ip);
	}

	/**
	 * 获取当前毫秒
	 *
	 * @param string $micro microtime()函数获取转转成毫秒数
	 * @return date
	 */
	public function getMicTime($micro = ""){
		$micro = (empty($micro))?microtime():$micro;
		$mtime = explode(' ', $micro);
		return $mtime[1] + $mtime[0];
	}

	/**
	 * URL重定向
	 *
	 * @param unknown_type $url		跳转地址
	 * @param unknown_type $time	将在几秒之后跳转
	 * @param unknown_type $msg		提示内容
	 */
	public function redirect($url,$time=0,$msg=''){
		//多行URL地址支持
		$url = str_replace(array("\n", "\r"), '', $url);
		if(empty($msg))
			$msg    =   "系统将在{$time}秒之后自动跳转到{$url}！";
		else
			$msg="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>提示信息</title><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"/><meta name=\"Generator\" content=\"EditPlus\"/><style>body{	font-family: 'Microsoft Yahei', Verdana, arial, sans-serif;	font-size:14px;}a{text-decoration:none;color:#174B73;}a:hover{ text-decoration:none;color:#FF6600;}h2{	border-bottom:1px solid #DDD;	padding:8px 0;    font-size:25px;}.title{	margin:4px 0;	color:#F60;	font-weight:bold;}.message,#trace{	padding:1em;	border:solid 1px #000;	margin:10px 0;	background:#FFD;	line-height:150%;}.message{	background:#FFD;	color:#2E2E2E;		border:1px solid #E0E0E0;}#trace{	background:#E7F7FF;	border:1px solid #E0E0E0;	color:#535353;}.notice{    padding:10px;	margin:5px;	color:#666;	background:#FCFCFC;	border:1px solid #E0E0E0;}.red{	color:red;	font-weight:bold;}</style></head><body><div class=\"notice\"><h2>提示信息 </h2><div ></div><p align=\"center\" id=\"trace\"><span class=\"title\">{$msg}</span><br />系统将在{$time}秒之后自动跳转 或 点击[<a href=\"{$url}\">这里</a>]</p></div></body></html><script language=\"javascript\">var top=parent.topFrame;if(typeof(top)=='object'){var loadMsg=top.document.getElementById('loadMsg');if(loadMsg!=undefined){loadMsg.style.display='none';}}</script>";
		if (!headers_sent()) {
			// redirect
			if(0===$time) {
				header("Location: ".$url);
			}else {
				header("refresh:{$time};url={$url}");
				echo($msg);
			}
			exit();
		}else {
			$str    = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
			if($time!=0)
			$str   .=   $msg;
			exit($str);
		}
	}
	
	public function printTree($data,$key,$prefix="|"){
		if(!isset($data[$key]) || !is_array($data[$key]) || count($data[$key]) == 0) return "";
		foreach($data[$key] as $val){
			echo "<option value=\"{$val['val']}\" title=\"{$val['name']}\" id = \"tree_{$val['id']}\">{$prefix}{$val['name']}</option>";
			LianPhp::expand()->Tool->printTree($data,$val['id'],substr($prefix,1)."&nbsp;&nbsp;&nbsp;&nbsp;|");
		}
	}
}
?>